//
//  CACategoryViewController.m
//  LNH
//
//  Created by twentylx on 2018/2/6.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CACategoryViewController.h"
#import "CACategoryTableViewCell.h"
#import "CACategoryApi.h"
#import "CAGoodsListViewController.h"

@interface CACategoryViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *cateTableView;
@property (nonatomic, strong) NSArray *categoryArray;

@end

static NSString *cateIdentifier = @"CateCell";

@implementation CACategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/// 初始化数据
- (void)lm_initialDefaultsForController {
    [self loadCategory];
}

/// 绑定 vm
- (void)lm_bindViewModelForController {
    
}

/// 创建视图
- (void)lm_createViewForController {
    [self.view setNeedsUpdateConstraints];
    
    [self.view addSubview:self.cateTableView];
}

/// 配置导航栏
- (void)lm_configNavigationForController {
    self.navigationItem.title = @"分类";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : RGBCOLOR(35, 24, 21)};
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
}

#pragma mark layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
    [_cateTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark get & set
- (UITableView *) cateTableView{
    if (!_cateTableView) {
        _cateTableView = [UITableView new];
        _cateTableView.dataSource = self;
        _cateTableView.delegate = self;
        _cateTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_cateTableView registerClass:[CACategoryTableViewCell class] forCellReuseIdentifier:cateIdentifier];        
    }
    return _cateTableView;
}

#pragma mark private method
- (void) loadCategory {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    CACategoryApi *categoryApi = [[CACategoryApi alloc] init];
    [categoryApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        CACategoryApi *api = (CACategoryApi *) request;
        if (api.done) {
            self.categoryArray = api.responseObject[@"result"];
            [self.cateTableView reloadData];
        } else {
            [SVProgressHUD showErrorWithStatus:api.errorMsg];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
    }];
}

#pragma mark - Table view DataSource & Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _categoryArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        CACategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cateIdentifier forIndexPath:indexPath];
        NSDictionary *dict = _categoryArray[indexPath.row];
        
        NSString *imageUrl = [NSString stringWithFormat:@"%@/%@", BASE_IMAGE_DOMAIN, dict[@"img"]];
        [cell.cateImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
        cell.titleLabel.text = dict[@"name"];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.categoryArray[indexPath.row];
    CAGoodsListViewController *goodsListVC = [CAGoodsListViewController new];
    goodsListVC.vcTitle = dict[@"name"];
    goodsListVC.category = [NSString stringWithFormat:@"%ld", [dict[@"id"] integerValue]];
    goodsListVC.isYPH = @"0"; 
    goodsListVC.isVip = @"0";
    goodsListVC.isSecond = @"0";
    goodsListVC.goodsName = @"";
    [self.navigationController pushViewController:goodsListVC animated:YES];
}

@end
